package system

import (
	"catering/global"
	"catering/model/system"

	"github.com/pkg/errors"
	"gorm.io/gorm"
)

var Api = new(api)

type api struct{}

func (a *api) TableName() string {
	return "sys_apis"
}

func (a *api) Initialize() error {
	entities := []system.SysApi{
		{ApiGroup: "base", Method: "POST", Path: "/base/login", Description: "用户登录(必选)"},

		{ApiGroup: "系统用户", Method: "DELETE", Path: "/user/deleteUser", Description: "删除用户"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/register", Description: "用户注册(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/getUserList", Description: "获取用户列表"},
		{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setUserInfo", Description: "设置用户信息"},
		{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setSelfInfo", Description: "设置自身信息(必选)"},
		{ApiGroup: "系统用户", Method: "GET", Path: "/user/getUserInfo", Description: "获取自身信息(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthorities", Description: "设置权限组"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/changePassword", Description: "修改密码（建(选择)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthority", Description: "修改用户角色(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/resetPassword", Description: "重置用户密码"},

		{ApiGroup: "api", Method: "POST", Path: "/api/createApi", Description: "创建api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/deleteApi", Description: "删除Api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/updateApi", Description: "更新Api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getApiList", Description: "获取api列表"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getAllApis", Description: "获取所有api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getApiById", Description: "获取api详细信息"},
		{ApiGroup: "api", Method: "DELETE", Path: "/api/deleteApisByIds", Description: "批量删除api"},

		{ApiGroup: "角色", Method: "POST", Path: "/authority/copyAuthority", Description: "拷贝角色"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/createAuthority", Description: "创建角色"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/deleteAuthority", Description: "删除角色"},
		{ApiGroup: "角色", Method: "PUT", Path: "/authority/updateAuthority", Description: "更新角色信息"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/getAuthorityList", Description: "获取角色列表"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/setDataAuthority", Description: "设置角色资源权限"},

		{ApiGroup: "casbin", Method: "POST", Path: "/casbin/updateCasbin", Description: "更改角色api权限"},
		{ApiGroup: "casbin", Method: "POST", Path: "/casbin/getPolicyPathByAuthorityId", Description: "获取权限列表"},

		{ApiGroup: "菜单", Method: "POST", Path: "/menu/addBaseMenu", Description: "新增菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenu", Description: "获取菜单树(必选)"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/deleteBaseMenu", Description: "删除菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/updateBaseMenu", Description: "更新菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuById", Description: "根据id获取菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuList", Description: "分页获取基础menu列表"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuTree", Description: "获取用户动态路由"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuAuthority", Description: "获取指定角色menu"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/addMenuAuthority", Description: "增加menu和角色关联关系"},

		{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/findFile", Description: "寻找目标文件（秒传）"},
		{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinue", Description: "断点续传"},
		{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinueFinish", Description: "断点续传完成"},
		{ApiGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/removeChunk", Description: "上传完成移除文件"},

		{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/upload", Description: "文件上传示例"},
		{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/deleteFile", Description: "删除文件"},
		{ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/getFileList", Description: "获取上传文件列表"},

		{ApiGroup: "操作记录", Method: "POST", Path: "/sysOperationRecord/createSysOperationRecord", Description: "新增操作记录"},
		{ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/findSysOperationRecord", Description: "根据ID获取操作记录"},
		{ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/getSysOperationRecordList", Description: "获取操作记录列表"},
		{ApiGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecord", Description: "删除操作记录"},
		{ApiGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecordByIds", Description: "批量删除操作历史"},

		{ApiGroup: "excel", Method: "POST", Path: "/excel/importExcel", Description: "导入excel"},
		{ApiGroup: "excel", Method: "GET", Path: "/excel/loadExcel", Description: "下载excel"},
		{ApiGroup: "excel", Method: "POST", Path: "/excel/exportExcel", Description: "导出excel"},
		{ApiGroup: "excel", Method: "GET", Path: "/excel/downloadTemplate", Description: "下载excel模板"},

		{ApiGroup: "城市", Method: "GET", Path: "/citys/page", Description: "获取城市列表"},
		{ApiGroup: "城市", Method: "POST", Path: "/city/create", Description: "创建城市信息"},
		{ApiGroup: "城市", Method: "POST", Path: "/city/update", Description: "更新城市信息"},
		{ApiGroup: "城市", Method: "DELETE", Path: "/city/delete", Description: "删除城市信息"},
		{ApiGroup: "城市", Method: "GET", Path: "/citys/province", Description: "根据省份获取城市列表"},

		{ApiGroup: "省份", Method: "GET", Path: "/provinces/page", Description: "获取省份列表"},
		{ApiGroup: "省份", Method: "GET", Path: "/provinces", Description: "获取所有省份"},
		{ApiGroup: "省份", Method: "POST", Path: "/province/create", Description: "创建省份信息"},
		{ApiGroup: "省份", Method: "POST", Path: "/province/update", Description: "更新省份信息"},
		{ApiGroup: "省份", Method: "DELETE", Path: "/province/delete", Description: "删除省份信息"},

		{ApiGroup: "区域", Method: "GET", Path: "/districts/page", Description: "获取区域列表"},
		{ApiGroup: "区域", Method: "GET", Path: "/districts/city", Description: "根据区域获取城市信息"},
		{ApiGroup: "区域", Method: "POST", Path: "/district/create", Description: "创建区域信息"},
		{ApiGroup: "区域", Method: "POST", Path: "/district/update", Description: "更新区域信息"},
		{ApiGroup: "区域", Method: "DELETE", Path: "/district/delete", Description: "删除区域信息"},

		{ApiGroup: "店铺详情", Method: "GET", Path: "/shops/page", Description: "获取店铺详情列表"},
		{ApiGroup: "店铺详情", Method: "POST", Path: "/shop/create", Description: "创建店铺详情"},
		{ApiGroup: "店铺详情", Method: "POST", Path: "/shop/update", Description: "更新店铺详情"},
		{ApiGroup: "店铺详情", Method: "DELETE", Path: "/shop/delete", Description: "删除店铺详情"},

		{ApiGroup: "店铺分类", Method: "GET", Path: "/shop/categorys/page", Description: "获取店铺分类列表"},
		{ApiGroup: "店铺分类", Method: "GET", Path: "/shop/categorys", Description: "获取所有店铺分类"},
		{ApiGroup: "店铺分类", Method: "POST", Path: "/shop/category/create", Description: "创建店铺分类"},
		{ApiGroup: "店铺分类", Method: "POST", Path: "/shop/category/update", Description: "更新店铺分类"},
		{ApiGroup: "店铺分类", Method: "DELETE", Path: "/shop/category/delete", Description: "删除店铺分类"},

		{ApiGroup: "店铺商品", Method: "GET", Path: "/shop/products/page", Description: "获取店铺商品列表"},
		{ApiGroup: "店铺商品", Method: "POST", Path: "/shop/product/create", Description: "创建店铺商品"},
		{ApiGroup: "店铺商品", Method: "POST", Path: "/shop/product/update", Description: "更新店铺商品"},
		{ApiGroup: "店铺商品", Method: "DELETE", Path: "/shop/product/delete", Description: "删除店铺商品"},

		{ApiGroup: "代金券", Method: "GET", Path: "/vouchers/page", Description: "获取代金券列表"},
		{ApiGroup: "代金券", Method: "GET", Path: "/vouchers/getLogs", Description: "获取代金券领取明细"},
		{ApiGroup: "代金券", Method: "POST", Path: "/voucher/create", Description: "创建代金券"},
		{ApiGroup: "代金券", Method: "POST", Path: "/voucher/update", Description: "更新代金券"},
		{ApiGroup: "代金券", Method: "DELETE", Path: "/voucher/delete", Description: "删除代金券"},

		{ApiGroup: "优惠券", Method: "GET", Path: "/coupons/page", Description: "获取优惠券列表"},
		{ApiGroup: "优惠券", Method: "GET", Path: "/coupons/getLogs", Description: "获取优惠券领取明细"},
		{ApiGroup: "优惠券", Method: "POST", Path: "/coupon/create", Description: "创建优惠券"},
		{ApiGroup: "优惠券", Method: "POST", Path: "/coupon/update", Description: "更新优惠券"},
		{ApiGroup: "优惠券", Method: "DELETE", Path: "/coupon/delete", Description: "删除优惠券"},

		{ApiGroup: "用户积分", Method: "GET", Path: "/integrations/page", Description: "获取用户积分列表"},
		{ApiGroup: "用户积分", Method: "GET", Path: "/integrations/logs", Description: "获取用户积分明细"},
		{ApiGroup: "用户积分", Method: "POST", Path: "/integration/change", Description: "更改用户积分"},

		{ApiGroup: "用户地址", Method: "GET", Path: "/user/addresses/page", Description: "获取用户地址列表"},
		{ApiGroup: "用户地址标签", Method: "GET", Path: "/user/address/tags/page", Description: "获取用户地址标签列表"},
		{ApiGroup: "用户地址标签", Method: "POST", Path: "/user/address/tag/create", Description: "创建用户地址标签"},
		{ApiGroup: "用户地址标签", Method: "POST", Path: "/user/address/tag/update", Description: "更新用户地址标签"},
		{ApiGroup: "用户地址标签", Method: "DELETE", Path: "/user/address/tag/delete", Description: "删除用户地址标签"},

		{ApiGroup: "用户VIP等级", Method: "GET", Path: "/vip/levels/page", Description: "获取用户VIP等级列表"},
		{ApiGroup: "用户VIP等级", Method: "GET", Path: "/vip/levels/logs", Description: "获取用户VIP等级明细"},
		{ApiGroup: "用户VIP等级", Method: "POST", Path: "/vip/level/create", Description: "创建用户VIP等级"},
		{ApiGroup: "用户VIP等级", Method: "POST", Path: "/vip/level/update", Description: "更新用户VIP等级"},
		{ApiGroup: "用户VIP等级", Method: "DELETE", Path: "/vip/level/delete", Description: "删除用户VIP等级"},
	}
	if err := global.DB.Create(&entities).Error; err != nil {
		return errors.Wrap(err, a.TableName()+"表数据初始化失败!")
	}
	return nil
}

func (a *api) CheckDataExist() bool {
	if errors.Is(global.DB.Where("path = ? AND method = ?", "/excel/downloadTemplate", "GET").First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
		return false
	}
	return true
}
